# -*- coding: utf-8 -*- 
"""
@Author: 孟颖
@email: 652044581@qq.com
@date: 2023/4/16 19:55
@desc: 文件储存服务接口封装
"""
from config import config
from minio import Minio
import os

# 部署minio
"""
docker run -p 8055:9000 --name minio1 \
-e "MINIO_ROOT_USER=Q3AM3UQ867SPQQA43P2F" \
-e "MINIO_ROOT_PASSWORD=tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" \
-v /mnt/data:/data \
-v /mnt/config:/root/.minio \
minio/minio server /data
"""


class MinioBucket:
    def __init__(self, client):
        self.client = client

    def create_bucket(self, bucket_name):
        """
        @desc: 创建桶
        @param bucket_name: 桶的名称
        @return:
        """
        return self.client.make_bucket(bucket_name)

    def bucket_exists(self, bucket_name):
        """
        @desc: 判断桶是否存在
        @param bucket_name: 桶的名称
        @return:
        """
        return self.client.bucket_exists(bucket_name)

    def bucket_delete(self, bucket_name):
        """
        @desc: 删除桶
        @param bucket_name: 桶的名称
        @return:
        """
        return self.client.remove_bucket(bucket_name)

    def bucket_list(self):
        """
        @return: 桶列表 [(bucket.name, bucket.creation_date)]
        """
        return self.client.list_buckets()


class MyMinio:

    def __init__(self, endpoint="", access_key="", secret=""):
        self.endpoint = endpoint or config.MINIO_HOST + ":" + str(config.MINIO_PORT)
        self.access_key = access_key or config.MINIO_ACCESS_KEY
        self.secret = secret or config.MINIO_SECRET_KEY
        self.bucket_name = config.PROJECT_NAME

        self.client = Minio(
            endpoint=self.endpoint,
            access_key=self.access_key,
            secret_key=self.secret,
            secure=False)

        # 创建桶
        minioBucket = MinioBucket(self.client)
        if not minioBucket.bucket_exists(self.bucket_name):
            minioBucket.create_bucket(self.bucket_name)

    def get_bucket(self):
        """获取桶对象"""
        return MinioBucket(self.client)

    def upload_file(self, file_name, file_path):
        """
        @param file_name: 保存的名字
        @param file_path: 文件的byte或者路径
        @return:
        """
        with open(file_path, "rb") as file_data:
            bytes_length = os.path.getsize(file_path)
            responses = self.client.put_object(self.bucket_name, file_name, file_data, bytes_length)

        return responses.etag

    def download_file(self, file_name, file_path):
        """
        @param file_name: 下载文件
        @param file_path: 下载文件的名称，绝对路径
        @return:
        """

        self.client.fget_object(self.bucket_name, file_name, file_path)
        return file_path

    def get_preview_url(self, file_name):
        """
        @param file_name: 获取文件预览连接
        @return:
        """
        url = self.client.presigned_get_object(self.bucket_name, file_name)
        return url

    def file_list(self):
        """
        @return:返回桶里面的文件列表
        """
        return self.client.list_objects(self.bucket_name, recursive=True)



if __name__ == '__main__':
    myMino = MyMinio()

    # 桶对象
    minioBucket = myMino.get_bucket()
    # print(minioBucket.create_bucket("o2oa"))
    # print(minioBucket.bucket_exists("o2oa"))

    # 上传
    print(minioBucket.bucket_list())
    # for i in range(0, 1000):
    #     import uuid
    #     file_name = uuid.uuid4().hex + ".pdf"
    #     print(file_name)
    #     print(myMino.upload_file(file_name, r"C:\Users\my\Desktop\冉冉的食谱注意对象.docx"))

    # 下载
    # print(myMino.download_file("123123.png", r"C:\Users\my\Desktop\傲雄发布变更手册V1.213.pdf"))

    # 预览
    # print(myMino.get_preview_url("0fd80f7533354dd19e87af392ba4fa31.pdf"))


    # 列表
    for file in myMino.file_list():
        print(file) # 返回文件对象